//
//  StarPredictViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "StarPredictViewController.h"
#import "StarPredictCell.h"
#import "PredictListModel.h"
@interface StarPredictViewController ()<UITableViewDelegate,UITableViewDataSource,StarPredictCellDelegate>{
    NSMutableArray *_predictArr;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSUInteger pageNum;
@end

@implementation StarPredictViewController


#pragma mark -- 预播列表网络请求
- (void)predictListRequset {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN,
                           @"page":@"1",
                           @"amount":@"10"
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kPredictList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        NSArray *array = responseObject[@"data"][@"list"];
        for (NSDictionary *dic in array) {
            PredictListModel *predictModel = [[PredictListModel alloc] initWithDict:dic];
            [_predictArr addObject:predictModel];
        }
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

#pragma mark -- 订阅网络请求
- (void)BookLivingRequestWithPredictID:(NSString *)prdictId IndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"follow":prdictId,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kBookPredict];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            PredictListModel *model = _predictArr[indexPath.row];
            model.l_booked = KString(responseObject[@"data"][@"number"]);
            model.booked = responseObject[@"data"][@"status"];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [self showTitle:responseObject[@"data"][@"message"]];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _predictArr = [NSMutableArray arrayWithCapacity:1];
    [self predictListRequset];
    self.navigationItem.title = @"预播列表";
    [self setUpUI];
    // Do any additional setup after loading the view.
}

#pragma mark -- UI
- (void)setUpUI {
    self.pageNum = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-kNAV_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _predictArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StarPredictCell *cell = [StarPredictCell loadNibCellWithTabelView:tableView];
    PredictListModel *model = _predictArr[indexPath.row];
    cell.predictListModel = model;
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 115;
}


#pragma mark -- StarPredictCellDelegate
- (void)PredictLivingWithCell:(StarPredictCell *)starPredictCell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:starPredictCell];
    PredictListModel *model = _predictArr[indexPath.row];
    [self BookLivingRequestWithPredictID:model.l_id IndexPath:indexPath];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
